/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.remotec.cs;

import com.sun.image.codec.jpeg.JPEGCodec;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.remotec.cs
 * @author:LiBo/Alex
 * @create-date:2020-04-06 22:46
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class MonitorServer {


    public static class Server extends Thread {
        private Dimension screenSize;
        private Rectangle rectangle;
        private Robot robot;

        public Server() {
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            rectangle = new Rectangle(screenSize);
            // 可以指定捕获屏幕区域  
            try {
                robot = new Robot();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e);
            }
        }

        @Override
        public void run() {
            ZipOutputStream os = null;
            Socket socket = null;
            while (true) {
                try {
                    socket = new Socket("192.168.1.100", 5001);
                    // 连接远程IP  23.
                    BufferedImage image = robot.createScreenCapture(rectangle);// 捕获制定屏幕形
                    os = new ZipOutputStream(socket.getOutputStream());
                    os = new ZipOutputStream(new FileOutputStream("C:/1.zip"));
                    // 加入压缩流  25.                
                    os.setLevel(9);
                    os.putNextEntry(new ZipEntry("test.jpg"));
                    JPEGCodec.createJPEGEncoder(os).encode(image);
                    // 图像编码成JPEG  30.                
                    os.close();
                    Thread.sleep(50);
                    // 每秒20帧
                } catch (IOException | InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
